package com.suning.sawp.web.controller;

import java.security.Principal;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.constants.LoggerConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.custcommission.CustCommissionInfo;
import com.suning.sawp.intf.esb.CustCommissionService;
import com.suning.sawp.web.exception.MobileException;
import com.suning.sawp.web.exception.MyGsonView;
import com.suning.vgs.follow.remote.intf.StoreManRemoteService;
import com.suning.vgs.follow.remote.store.StoreManEbuyAccount;

/**
 * 
 * 客户佣金
 *
 * @author 15050769
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@RequestMapping("/commission")
public class CustCommissionController extends DjBaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstants.SAWP_SERVICE);

    @Autowired
    CustCommissionService custCommissionService;

    @Autowired
    StoreManRemoteService storeManRemoteService;

    /**
     * 
     *  查询客户佣金
     *
     * @param deviceNo
     * @param principal
     * @return
     * @throws MobileException
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/custCommission.do")
    public MyGsonView custCommission(String deviceNo, Principal principal) throws MobileException {
        ReturnMsg<List<CustCommissionInfo>> returnMsg = new ReturnMsg<List<CustCommissionInfo>>();
        Object[] args = { principal.getName() };
        LOGGER.info("custCommission staffId:{}", args);

        // 获取会员编码
        StoreManEbuyAccount storeManEbuyAccount = storeManRemoteService.queryStoreManEbuyAccount(principal.getName());
        //校验是否绑定易购账号
        if (storeManEbuyAccount == null || StringUtils.isBlank( storeManEbuyAccount.getCustNo())) {
            LOGGER.error("custCommission custNum is null for " + principal.getName());
            returnMsg.setError(ErrorCodeConstants.ERR_0036);
            return setGsonView(returnMsg);
        }
        String custNum = storeManEbuyAccount.getCustNo();

        // 校验参数合法性
        if (StringUtils.isBlank(custNum) || StringUtils.isBlank(deviceNo)) {
            LOGGER.error("custCommission custNum is null for " + principal.getName());
            returnMsg.setError(ErrorCodeConstants.ERR_0001);
            return setGsonView(returnMsg);
        }

        ReturnMsg<List<CustCommissionInfo>> result = custCommissionService.getCustCommission(custNum, deviceNo);

        if (result != null && ReturnMsg.SUCCESS.equals(result.getRetFlag())) {
            returnMsg.setData(result.getData());
        } else {
            LOGGER.error("querySchemeForCust  input result fail  for staffid  " + principal.getName());
            returnMsg.setError(ErrorCodeConstants.ERR_0028);
        }
        return setGsonView(returnMsg);
    }

}
